// pages/searchgoods/searchgoods.js

var that,
  api = new(require('../../utils/api.js')),
  headimg = api.getimgurl(),
  app = getApp(),
  util = new(require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: '',
    goodsname: '',
    pagesize: 20,
    start: 0,
    goodslist: [],
    name: '',
    success: 3,
    headimg,
    showmodal: 0,
    buysrc:'',
    buyprice:'',
    buystore:'',
    goodsnum:'',
    count: 1,
    textcontent:`没有找到你想要的商品\n`+`换个搜索词试试`,
    istap:'',
    orgingoodslist:[],
    direction:1,
    uppath:'../../image/up.png',
    downpath:'../../image/down.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    that = this
    let {page} = options
    that.setData({ page })
  },
//点击输入框
  tapinput(){
    that.setData({
      success: 3,
      page:1,
    })
  },
    //加入购物车
    addshopcar(){
      let { count,goodsdata }=that.data,paynum='',cartids=wx.getStorageSync('cartids')
      paynum=count
      let data={  paynum,type:'GOODS',goodsid:goodsdata.id}
        api.addcart(data,res=>{
          if(res.data.issuccess == 1){
            wx.showToast({
              title: '加入购物车成功',
              icon: 'none',
              duration: 2000,
              success(){
                cartids.push(goodsdata)
                app.badege(cartids)
                wx.setStorageSync("cartids",cartids)
              }
            })
            that.setData({
              showmodal:0
            })
          }else{
            wx.showToast({
              title: '加入购物车失败',
              icon: 'none',
              duration: 2000
            })
          }
          })
      // }
    },
  //关闭购物车弹窗
  closechose() {
    that.setData({
      showmodal: !1,
      anim: ''
    })
  },

  //购物车数量
  counttap(e) {
    let {
      count,buystore,goodsdata
    } = that.data
    if (e.currentTarget.dataset.code == 1) {
     if(count<goodsdata.kc){
       count++
     }
     else{
       wx.showToast({
         title: '商品库存不足',
         icon: 'none',
         duration: 2000
       })
     }
    } else {
      if (count > 1) {
        count--
      } else {
        wx.showToast({
          title: '至少添加一件商品',
          icon: 'none',
          duration: 2000
        })
      }
    }
    let buyprice =(Number(that.data.price)*count).toFixed(2)
    that.setData({
      buyprice,
      count
    })
  },

  //显示购物车弹窗
  showcarmodel(e){
    let {id,item} = e.currentTarget.dataset,{buysrc,buyprice,buystore,goodsnum,usertype,count} = that.data
    that.setData({
      buyprice: (item.tygprice/100).toFixed(2),
      price: (item.promotionprice/100).toFixed(2),
      totalscore:item.tygscore,
      score:item.tygscore,
      count:1,
      anim: 'sizewrap 0.3s',
      goodsdata:item
    })
    that.setData({
      showmodal:1,
    },()=>{      
   
    })
  },





  //商品详情
  todetail(e) {

    let {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '../goodsDetail/goodsDetail?id=' + id,
    })
  },
  // 取消
  reback() {
    wx.navigateBack({

    })
    // wx.({
    //   url: '../index/index',
    // })
  },

  //商品搜索名字
  searchgoodsname(e) {

    that.setData({
      keyword: e.detail.value
    })

  },
  //商品搜索
  searchgoods() {
    let { keyword, pagesize,start  } = that.data,goodslist=[],orgingoodslist=[]
    api.getgoodslist({keyword, pagesize,start}, res => {
      if (res.data.issuccess == 1) {
        if (res.data.list.length>0){
          res.data.list.forEach(x=>{
            x['newprice'] = (x.promotionprice / 100).toFixed(2);
            goodslist.push(x)
            orgingoodslist.push(x)
          })
          that.setData({
            goodslist,
            orgingoodslist,
            page: 0,
            goodsname: '',
            success: 1
          })
        } else {
          that.setData({
            page: 0,
            goodsname: '',
            success: 0
          })
        }
       
      } 
    })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})